import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Ping {
	private static String url = null, str1 = null, str2 = null;
	private static int n = 0, l = 0;
	private static String[] str = null;
	private static Process p = null;

	public static void main(String[] args) {
		/*
		 * if (args.length != 3) {
		 * System.out.println("Incorrect number of arguments specified");
		 * System.out .println(
		 * "Syntax is >> java Ping <url like www.google.com> <interval> <data packets>"
		 * ); System.out.println("Please retry"); System.exit(0); }
		 */
		try {
			url = "www.google.com";// args[0];
			try {
				n = 100;// Integer.parseInt(args[1]);
				l = 100;// Integer.parseInt(args[2]);
			} catch (NumberFormatException nfe) {
				System.out
						.println("Value of either 'n' or 'l' is not in integer formats");
				System.out.println("Please retry");
				System.exit(0);
			}
			str = new String[] { "ping.exe", url, "-n", n + "", "-l", l + "" };

			p = Runtime.getRuntime().exec(str);

			StringBuilder sbx = new java.lang.StringBuilder();
			for (String x : str)
				sbx.append(x);

			System.out.println(sbx.toString());

			BufferedReader br = new BufferedReader(new InputStreamReader(
					p.getInputStream()));
			while ((str1 = br.readLine()) != null) {
				System.out.println(str1);
				if (str1.contains("Minimum")) {
					str2 = str1;
					break;
				}
			}

			/* str2 will obviously have Minimum, Maximum, Average */
			if (str2 != null) {
				if (str2.contains("Minimum")) {
					str2 = str2.trim();
					int avg = Integer.parseInt(str2
							.substring(str2.indexOf("Average") + 9,
									str2.length()).replace("ms", "").trim());
					System.out.println("Average: " + avg);
					System.out.println("Throughput (KB/s): "
							+ (l * 1000000 / (avg)) / 1024);
				} else {
					System.out.println("Exception ONE");
					System.out
							.println("Could not reach target server. Try to increase interval number.");
					System.out.println("Please retry");
				}
			} else {
				System.out.println("Exception TWO");
				System.out
						.println("Could not reach target server. Try to increase interval number.");
				System.out.println("Please retry");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}